package com.liuyan.controller;

import com.liuyan.pojo.Content;
import com.liuyan.pojo.User;
import com.liuyan.service.IContentService;
import com.liuyan.service.IUserService;
import com.liuyan.util.MD5;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.liuyan.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("contentController")
public class ContentController extends BaseController {
    @Resource
    private IContentService contentService;
    @Resource
    private IUserService userService;

    /**
     * 用户提交内容
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(params = "method=submitContent")
    public String submitContent(HttpServletRequest request, HttpServletResponse response,Model model) {
        HttpSession session = request.getSession();
        Object obj  = session.getAttribute("userId");//统一判断用户是否登录，若没登录，给出提示，并且回到登录页面
        if(obj!=null) {
            int userId = Integer.parseInt(session.getAttribute("userId").toString());
        /*创建contentBean，得到数据，并且往bean中set值*/
            Content contentBean = new Content();
            String content = request.getParameter("comment");
            contentBean.setUserId(userId);
            contentBean.setContent(content);
            contentBean.setLevel(1);//level默认是1
            contentBean.setFloor(this.contentService.getMaxFloor() + 1);//floor最大楼数+1
            boolean submit = this.contentService.writeContent(contentBean);//插入数据
            if (submit) {
                writeText(response, CommonConstants.RESULT_SUCCESS);
            } else {
                writeText(response, CommonConstants.RESULT_FAIL);
            }
        }else {
            model.addAttribute("message","noLogin");
            return "login";
        }
        return null;
    }

    /**
     * 回复
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(params = "method=response")
    public String response(HttpServletRequest request,HttpServletResponse response,Model model) {
        HttpSession session = request.getSession();
        Object obj  = session.getAttribute("userId");
        if(obj!=null) {
            int userId = Integer.parseInt(session.getAttribute("userId").toString());
            Content contentBean = new Content();
            String content = request.getParameter("comment");
            String floorStr = request.getParameter("floor");
            contentBean.setUserId(userId);
            contentBean.setContent(content);
            contentBean.setLevel(2);
            contentBean.setFloor(Integer.parseInt(floorStr));
            boolean isResponse = this.contentService.writeContent(contentBean);//插入回复数据
            if (isResponse) {
                writeText(response, CommonConstants.RESULT_SUCCESS);
            } else {
                writeText(response, CommonConstants.RESULT_FAIL);
            }
        }else {
            model.addAttribute("message","noLogin");
            return "login";
        }
        return null;

    }

    /**
     * 删除内容根据contentid
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(params = "method=deleteContent")
    public String deleteContent(HttpServletRequest request, HttpServletResponse response,Model model) {
        HttpSession session = request.getSession();
        Object obj  = session.getAttribute("userId");
        if(obj!=null) {
            int contentId = Integer.parseInt(request.getParameter("contentId"));//得到contentId
            int level = this.contentService.getLevelByContentId(contentId);
            if (level == 2) {
                boolean isDel = this.contentService.deleteContent(contentId);//根据contentId删除
                if (isDel) {
                    writeText(response, CommonConstants.RESULT_SUCCESS);
                } else {
                    writeText(response, CommonConstants.RESULT_FAIL);
                }
            } else {
                boolean isDelFloor = this.contentService.deleteContentByFloor(contentId);
                if (isDelFloor) {
                    writeText(response, CommonConstants.RESULT_SUCCESS);
                } else {
                    writeText(response, CommonConstants.RESULT_FAIL);
                }
            }
        }else {
            model.addAttribute("message","noLogin");
            return "login";
        }
        return null;
    }

    /**
     * 根据页数分页查询
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(params = "method=listOfPage")
    public String listOfPage(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        Object obj  = session.getAttribute("userId");
        if(obj!=null) {
            int currentPage = Integer.parseInt(request.getParameter("currentPage"));//获得第几页
            List<User> userlist = this.userService.getUserList();
            List<Content> getContentOfPageForLevelOne = this.contentService.getContentOfPageForLevelOne(currentPage);
            /*先查找User的list，在查找content的list1，遍历content的list2，如果list2中bean中的userid等于list1中bean的id，那么就把list1中的bean的head赋值给list2中的bean的head*/
            for (int j = 0; j < getContentOfPageForLevelOne.size(); j++) {
                for (int i = 0; i < userlist.size(); i++) {
                    if (userlist.get(i).getId() == getContentOfPageForLevelOne.get(j).getUserId()) {
                        getContentOfPageForLevelOne.get(j).setHead(userlist.get(i).getHead());
                    }
                }
            }

            List<Content> getContentOfPageForLevelTwo = this.contentService.getContentOfPageForLevelTwo(currentPage);
            for (int j = 0; j < getContentOfPageForLevelTwo.size(); j++) {
                for (int i = 0; i < userlist.size(); i++) {
                    if (userlist.get(i).getId() == getContentOfPageForLevelTwo.get(j).getUserId()) {
                        getContentOfPageForLevelTwo.get(j).setHead(userlist.get(i).getHead());
                    }
                }
            }
            int count = this.contentService.getAllCount();//获得总数量，好进行分页
            model.addAttribute("currentPage", currentPage);
            model.addAttribute("count", count);
            model.addAttribute("contentListForLevelOne", getContentOfPageForLevelOne);
            model.addAttribute("contentListForLevelTwo", getContentOfPageForLevelTwo);
            return "index";
        }else {
            model.addAttribute("message","noLogin");
            return "login";
        }
    }

    /**
     * 验证密码
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(params = "method=getPwdCheck")
    public String getPwdCheck(HttpServletRequest request,HttpServletResponse response,Model model) {
        HttpSession session = request.getSession();
        Object obj  = session.getAttribute("userId");
        if(obj!=null) {
            int userId = Integer.parseInt(session.getAttribute("userId").toString());
            String passwordOrg=request.getParameter("password");
            String password = MD5.sign(passwordOrg, "", CommonConstants.SYSTEMTYPE);
            User user = new User();
            user.setId(userId);
            user.setPassword(password);
            boolean isPass = this.userService.getPwdCheck(user);
            if(isPass){
                writeText(response, CommonConstants.RESULT_SUCCESS);
            }else {
                writeText(response, CommonConstants.RESULT_FAIL);
            }
        }else {
            model.addAttribute("message","noLogin");
            return "login";
        }
        return null;
    }

    /**
     * 修改密码
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(params = "method=changePassword")
    public String changePassword(HttpServletRequest request,HttpServletResponse response,Model model) {
        HttpSession session = request.getSession();
        Object obj  = session.getAttribute("userId");
        if(obj!=null) {
            int userId = Integer.parseInt(session.getAttribute("userId").toString());
            String passwordOrg=request.getParameter("password");
            String password = MD5.sign(passwordOrg, "", CommonConstants.SYSTEMTYPE);
            User user = new User();
            user.setId(userId);
            user.setPassword(password);
            boolean isChange = this.userService.modifyPassword(user);
            if(isChange){
                writeText(response, CommonConstants.RESULT_SUCCESS);
            }else {
                writeText(response, CommonConstants.RESULT_FAIL);
            }
        }else {
            model.addAttribute("message","noLogin");
            return "login";
        }
        return null;
    }

    /**
     * 退出
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(params = "method=quit")
    public String quit(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        session.invalidate();
        return "login";
    }
}
